package com.rabbiter.oes.result;


import org.springframework.lang.NonNull;

public class R<T> {

    /**
     * 状态
     */
    private String status;
    /**
     * 状态消息
     */
    private String message;

    /**
     * 数据
     */
    private T data;


    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    private R() {
        status = ResultCode.SUCCESS.getCode();
        message = ResultCode.SUCCESS.getMessage();
    }

    public static <T> R<T> error() {
        return error(ResultCode.ERROR);
    }

    public static <T> R<T> error(String message) {
        R<T> r = new R<>();
        r.status = ResultCode.ERROR.getCode();
        r.message = message;
        return r;
    }

    public static <T> R<T> error(ResultCode code) {
        R<T> r = new R<>();
        r.status = code.getCode();
        r.message = code.getMessage();
        return r;
    }

    public static <T> R<T> error(String code, String message) {
        R<T> r = new R<>();
        r.status = code;
        r.message = message;
        return r;
    }

    public static <T> R<T> ok() {
        return new R<>();
    }

    public static <T> R<T> ok(String message) {
        R<T> r = new R<>();
        r.message = message;
        return r;
    }

    public static <T> R<T> ok(ResultCode resultCode) {
        R<T> r = new R<>();
        r.message = resultCode.getMessage();
        r.status = resultCode.getCode();
        return r;
    }

    public static <T> R<T> ok(String status, String message) {
        R<T> r = new R<>();
        r.message = message;
        r.status = status;
        return r;
    }

    public static <T> R<T> okOrError(boolean isTure, String successMessage, String errorMessage) {
        if (isTure) {
            return ok(successMessage);
        } else {
            return error(errorMessage);
        }

    }

    public static <T> R<T> logicError(String message) {
        R<T> r = new R<>();
        r.status = ResultCode.LOGIC_ERROR.getCode();
        r.message = message;
        return r;
    }

    /**
     * 新数据返回对像
     *
     * @param data 数据返回对像
     * @return R<T>
     */
    public R<T> data(@NonNull T data) {
        this.data = data;
        return this;
    }
}
